package com.begye.smartloan.apptest.shenhezhong

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.begye.smartloan.apptest.tool.IApiProcessCallback
import com.begye.smartloan.apptest.tool.doPostRequest
import org.json.JSONArray
import org.json.JSONObject

class ShenHeZhongViewModel : ViewModel() {

    private var pinJiaQiShu = 0

    private val _loadingStateLiveData = MutableLiveData<Boolean>()
    val onLoadingStateLiveData get() = _loadingStateLiveData

    private val _needPingJiaStateLiveData = MutableLiveData<Boolean>()
    val onNeedPingJiaStateLiveData get() = _needPingJiaStateLiveData

    private val _qianBaoListLiveData = MutableLiveData<List<JSONObject>>()
    val onQianBaoListLiveData get() = _qianBaoListLiveData

    fun didShowPingJiaKuan() {
        pinJiaQiShu += 1
    }

    fun canShowPingJiaKuan(): Boolean {
        return pinJiaQiShu <= 1
    }

    fun requestQianBaoList() {
        doPostRequest(
            path = "/v0v2c30oj7QBZsF1V0PQqgQ",
            action = hashMapOf(),
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONArray("obj") ?: JSONArray()

                        val list = mutableListOf<JSONObject>()
                        for (index in 0 until obj.length()) {
                            val item = obj.optJSONObject(index)
                            if (item != null) {
                                list.add(item)
                            }
                        }
                        _qianBaoListLiveData.value = list

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }

            })
    }

    fun requestPingJiaConfig() {
        doPostRequest(
            path = "/v1mvpSI35dkcwiy3Zy7iiuQ",
            action = hashMapOf(),
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                        val awiow = obj.optJSONObject("activeWindow") ?: JSONObject()
                        val aswi = awiow.optString("switch", "NO")

                        val nwiow = obj.optJSONObject("normalWindow") ?: JSONObject()
                        val nswi = nwiow.optString("switch", "NO")

                        if ((aswi == "YES") or (nswi == "YES")) {
                            println("------nswi:$nswi")
                            _needPingJiaStateLiveData.value = true
                        } else {
                            _needPingJiaStateLiveData.value = false
                        }

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }

            })
    }

}